/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean;

import BO.LabBO;
import BO.SignLabBO;
import BO.TimeTableBO;
import BO.UserBO;
import java.util.Date;
import java.util.Map;
import javax.faces.component.UIParameter;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import pojo.SignLab;
import pojo.User;

/**
 *
 * @author phong
 */
public class SignLabBean {

    private String labID;
    private String timeTableID;
    private String useDate;
    private String content;

    public SignLabBean() {
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getLabID() {
        return labID;
    }

    public void setLabID(String labID) {
        this.labID = labID;
    }

    public String getTimeTableID() {
        return timeTableID;
    }

    public void setTimeTableID(String timeTableID) {
        this.timeTableID = timeTableID;
    }

    public String getUseDate() {
        return useDate;
    }

    public void setUseDate(String useDate) {
        this.useDate = useDate;
    }

    public void viewSignLab(ActionEvent e) {
        UIParameter labId = (UIParameter) e.getComponent().findComponent("labId");
        UIParameter date = (UIParameter) e.getComponent().findComponent("useDate");
        UIParameter timeTableId = (UIParameter) e.getComponent().findComponent("timeTableId");
        this.labID = labId.getValue().toString();
        this.useDate = date.getValue().toString();
        this.timeTableID = timeTableId.getValue().toString();
    }

    public void denySignLab(ActionEvent e) {
        UIParameter labId = (UIParameter) e.getComponent().findComponent("signDenyID");
        int id = (Integer) (labId.getValue());
        SignLab signLab = SignLabBO.getSignLabByID(id);
        signLab.setStatus("Deny");
        SignLabBO.changeStatusSignLab(signLab);
    }

    public void approveSignLab(ActionEvent e) {
        UIParameter labId = (UIParameter) e.getComponent().findComponent("signApproveID");
        SignLab signLab = SignLabBO.getSignLabByID((Integer) (labId.getValue()));
        signLab.setStatus("Approve");
        SignLabBO.changeStatusSignLab(signLab);
    }

    public boolean checkFreeLab(String labID, String timeTableID, Date useDate) {
        return SignLabBO.checkFreeLab(labID, timeTableID, useDate);
    }

    public String doSignLab() {
        SignLab signLab = new SignLab();
        signLab.setLab(LabBO.getLabByID(labID));
        signLab.setTimeTable(TimeTableBO.getTimeTableByID(timeTableID));
        signLab.setContent(content);
        signLab.setUseDate(new Date(useDate));
        signLab.setCreateDate(new Date());
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map session = facesContext.getExternalContext().getSessionMap();
        User user = (User) session.get("User");
        signLab.setUser(UserBO.getUserByID(user.getId()));
        return SignLabBO.SignLab(signLab);
    }
}
